package hanjia;

import java.util.Arrays;

/**
 * Description:
 * Author:    Dane
 * CreateTime:2025/2/11-下午8:28
 * Since  :   1.0
 * Version:   1.0
 */
public class LeetCode654 {

    public static TreeNode constructMaximumBinaryTree(int[] nums) {
        if (nums.length==0){
            return null;
        }

        int maxIndex = findMaxIndex(nums);
        TreeNode root = new TreeNode(nums[maxIndex]);
        int[] left = Arrays.copyOfRange(nums, 0, maxIndex );
        int[] right = Arrays.copyOfRange(nums, maxIndex+1, nums.length);

        root.left=constructMaximumBinaryTree(left);
        root.right=constructMaximumBinaryTree(right);
        return root;



    }


    public static int findMaxIndex(int []nums){
        int max=0;
        int index=0;
      for (int i=0;i<nums.length;i++){
          if (nums[i]>max){
              max=nums[i];
              index=i;
          }
        }

      return index;
    }

    public static void main(String[] args) {
        int nums[] = {3,2,1,6,0,5};
        constructMaximumBinaryTree(nums);

    }

}
